using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

// Project Server 2007 References
using Microsoft.Office.Project.Server.Library;

namespace Microsoft.EPM.TSAutoStatus
{
    /// <summary>
    /// Statusing Helper Methods
    /// </summary>
    class StatusingPSI
    {
        /// <summary>
        /// Get list of assignments to submit
        /// </summary>
        /// <param name="timesheetDS">Timesheet dataset</param>
        /// <returns>List of assignements</returns>
        public static List<Guid> SubmitStatus(TimesheetWS.TimesheetDataSet timesheetDS, AppLog appLog, bool logEvents)
        {
            List<Guid> assignments = new List<Guid>();
            StatusingWS.StatusingDataSet statDS = new StatusingWS.StatusingDataSet();

            for (int i = 0; i < timesheetDS.Lines.Count; i++)
            {
                if ((timesheetDS.Lines[i].TS_LINE_VALIDATION_TYPE == (int)TimesheetEnum.ValidationType.Verified) &&
                    (timesheetDS.Lines[i].TS_LINE_ACT_SUM_VALUE > 0))
                {
                    assignments.Add(timesheetDS.Lines[i].ASSN_UID);
                    if (logEvents)
                    {
                        decimal hours = timesheetDS.Lines[i].TS_LINE_ACT_SUM_VALUE / 60000;
                        appLog.WriteLine(string.Format("   Timesheet line: {0}; Project: {1}; Assignement: {2}; Actual Work: {3}", i,
                            timesheetDS.Lines[i].TS_LINE_CACHED_PROJ_NAME,
                            timesheetDS.Lines[i].TS_LINE_CACHED_ASSIGN_NAME,
                            hours.ToString("N2")));
                    }
                }
            }
            return assignments;
        }
    }

    /// <summary>
    /// Perfom WS impersonation
    /// </summary>
    class StatusingDerived : StatusingWS.Statusing
    {
        private String contextString = String.Empty;

        protected override WebRequest GetWebRequest(Uri uri)
        {
            //here we are overriding the GetWebRequest method and adding the 2 web request headers
            WebRequest webRequest = base.GetWebRequest(uri);
            if (contextString != String.Empty)
            {
                webRequest.UseDefaultCredentials = true;
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                webRequest.Headers.Add("PjAuth", contextString);
                webRequest.Headers.Add("ForwardFrom", "/_vti_bin/psi/statusing.asmx");
                webRequest.PreAuthenticate = true;
            }
            return webRequest;
        }

        public void SetImpersonationContext(bool isWindowsUser, String userNTAccount, Guid userGuid, Guid trackingGuid, Guid siteId, String lcid)
        {
            contextString = GetImpersonationContext(isWindowsUser, userNTAccount, userGuid, trackingGuid, siteId, lcid);
        }

        private String GetImpersonationContext(bool isWindowsUser, String userNTAccount, Guid userGuid, Guid trackingGuid, Guid siteId, String lcid)
        {
            PSContextInfo contextInfo = new PSContextInfo(isWindowsUser, userNTAccount, userGuid, trackingGuid, siteId, lcid);
            return PSContextInfo.SerializeToString(contextInfo);
        }
    }
}
